﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineMealOrdering.Models;

namespace OnlineMealOrdering.Controllers
{
    public class ExceptionController : Controller
    {
        public ActionResult ShowGlobalException()
        {
            var model = new ErrorModel();
            var httpException = RouteData.Values["httpException"] as HttpException;
            var httpCode = (httpException == null) ? 500 : httpException.GetHttpCode();
            OnlineMealOrdering.Common.WebLog<ExecutionEngineException>.AddLog(string.Format("异常错误:{0},{1}", HttpContext.Request.UserHostAddress, HttpContext.Request.RawUrl));
            switch (httpCode)
            {
                case 403:
                    Response.StatusCode = 403;
                    model.Heading = "禁止访问";
                    model.Message = "你没有得到浏览该页面的授权。";
                    break;
                case 404:
                    Response.StatusCode = 404;
                    model.Heading = "网页找不到";
                    model.Message = "找不到你要浏览的网页。";
                    break;
                case 500:
                default:
                    Response.StatusCode = 500;
                    model.Heading = "错误";
                    model.Message = "抱歉，网页出现了错误，我们已经做了记录，并会尽快解决。";
                    break;
            }
            //turn off custrom errer setting for IE7.0
            Response.TrySkipIisCustomErrors = true;

            //save error log through log4net
            if (httpException != null)
            {
                //todo: add log
            }
            return View(model);
        }

    }
}
